/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.scuecsz.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *
 * @author Administrator
 */
public class ConnectionBuilder {
    private final static String JAVA_COMP_ENV = "java:comp/env";
    private final static String JNDI = "jdbc/SCUECSZORGDB";

    public static Connection getConnection() throws NamingException, SQLException {
        Connection conn = null;
        conn = ((DataSource) ((Context) new InitialContext().lookup(JAVA_COMP_ENV)).lookup(JNDI)).getConnection();
        return conn;
    }

    public static void closeConnection(Connection conn) {
        if (null != conn) {
            try {
                conn.close();
//                System.out.println("conn is closed");
            } catch (SQLException ex) {
            }
        }
    }

    public static void closePreparedStatement(PreparedStatement ps) {
        if (null != ps) {
            try {
                ps.close();
//                System.out.println("ps is closed");
            } catch (SQLException ex) {
            }
        }
    }

    public static void closeResultSet(ResultSet rs) {
        if (null != rs) {
            try {
                rs.close();
//                System.out.println("rs is closed");
            } catch (SQLException ex) {
            }
        }
    }
}
